package com.lzy.controller;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lzy.annotation.AuthCheck;
import com.lzy.annotation.DataScope;
import com.lzy.annotation.Log;
import com.lzy.common.R;
import com.lzy.common.base.BaseController;
import com.lzy.constant.Constants;
import com.lzy.context.LocalUser;
import com.lzy.entity.PageDomain;
import com.lzy.enums.BusinessType;
import com.lzy.security.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lzy.entity.SysUserEntity;
import com.lzy.service.ISysUserService;

/**
 * 用户信息表
 *
 * @author lyw
 * @email lyw@gmail.com
 * @date 2023-02-02 17:06:20
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("/sysuser")
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserService ISysUserService;

    /**
     * 列表
     */
    @AuthCheck(hasAuthority = "system:user:list")
    @PostMapping("/list")
    @ApiOperation("获取用户列表")
    public R list(@RequestBody SysUserEntity sysUserEntity) {

        IPage<Map<String, Object>> list = ISysUserService.selectPageListByConditional(sysUserEntity);
        return R.ok().put("records", list);
    }

    /**
     * 信息
     */
    @AuthCheck(hasRole = Constants.ADMIN)
    @GetMapping("/info/{userId}")
    @ApiOperation(value = "用户信息")
    public R info(@ApiParam("用户id") @PathVariable("userId") Long userId) {

        LoginUser sysUserVo = ISysUserService.selectUserInfoById(userId);
        return R.ok().put("sysUser", sysUserVo);
    }

    /**
     * 保存
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册用户")
    public R register(@Validated @RequestBody SysUserEntity sysUser) {

        ISysUserService.registerSysUser(sysUser);
        return R.ok();
    }

    /**
     * 新增用户
     */
    @AuthCheck(hasAuthority = "system:user:add")
    @ApiOperation("添加用户")
    @PostMapping("/add")
    public R add(@Validated @RequestBody SysUserEntity sysUser) {

        ISysUserService.addSysUser(sysUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @AuthCheck(hasAuthority = "system:user:edit")
    @PostMapping("/update")
    @ApiOperation(value = "更新用户")
    @Log(title = "用户模块", businessType = BusinessType.UPDATE)
    public R update(@Validated @RequestBody SysUserEntity sysUser) {

        ISysUserService.updateUserInfo(sysUser);
        return R.ok();
    }

    /**
     * 删除
     */
    @AuthCheck(hasAuthority = "system:user:remove")
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    public R delete(@RequestBody List<Long> userIds) {

        if (ArrayUtils.contains(userIds.toArray(), LocalUser.getLoginUser().getId()))
        {
            return R.error("当前用户不能删除");
        }
        ISysUserService.removeUserInfo(userIds);
        return R.ok();
    }

    /**
     * 重置密码
     */
    @AuthCheck(hasAuthority = "system:user:edit")
    @ApiOperation(value = "重置密码")
    @PutMapping("/resetPwd")
    public R resetPwd(@RequestBody SysUserEntity user) {

        ISysUserService.resetPwd(user);
        return R.ok();
    }

    /**
     * 状态修改
     */
    @AuthCheck(hasAuthority = "system:user:edit")
    @ApiOperation(value = "状态修改")
    @PostMapping("/changeStatus")
    public R changeStatus(@RequestBody SysUserEntity user) {
        ISysUserService.updateUserStatus(user);
        return R.ok();
    }

    /**
     * 根据用户编号获取授权角色
     */
    @AuthCheck(hasAuthority = "system:user:query")
    @GetMapping("/authRole/{userId}")
    @ApiOperation(value = "获取授权角色根据userId")
    public R authRole(@PathVariable("userId") Long userId) {

        List<String> roles = ISysUserService.getRolesByUserId(userId);
        return R.ok().put("records", roles);
    }

    /**
     * 用户授权角色
     */
    @PostMapping("/authRole")
    @AuthCheck(hasAuthority = "system:user:edit")
    @ApiOperation(value = "添加用户授权角色")
    public R insertAuthRole(Long userId, Long[] roleIds) {
        ISysUserService.insertUserAuth(userId, roleIds);
        return R.ok();
    }

    /**
     * 修改个人密码
     */
    @PostMapping("/changePwd")
    @ApiOperation(value = "修改个人密码")
    public R updateUserPwd(String originPwd, String password) {
        ISysUserService.changeUserPwd(originPwd, password);
        return R.ok();
    }
}
